package it.traveldream.beans;

import java.util.ArrayList;
import java.util.List;

import it.traveldream.entities.Componente;
import it.traveldream.entities.Pacchetto;
import it.traveldream.entities.Pacchetto_Personal;
import it.traveldream.entities.Trasporto;
import it.traveldream.entities.User;
import it.traveldream.exception.PacchettoNonValidoException;
import it.traveldream.interfaces.ConflictBeanInterface;
import it.traveldream.interfaces.ForClientInterface;
import it.traveldreamClient.beans.ClientManagerInterface;
import it.traveldreamClient.beans.dto.ComponenteDTO;
import it.traveldreamClient.beans.dto.PkgDTO;
import it.traveldreamClient.beans.dto.PkgPersonalDTO;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 * Session Bean implementation class ClientManager
 */
@Stateless
public class ClientManager implements ClientManagerInterface {
	
	@EJB
	private ForClientInterface pkg_mgr;
	@EJB
	private ConflictBeanInterface cnf;
	@PersistenceContext EntityManager em;
	
	
	@Resource
	private EJBContext context;
	
	
	
    /**
     * Default constructor. 
     */
    public ClientManager() {
    }
    
    
    public void modificaPacchetto(PkgDTO pkg) throws PacchettoNonValidoException{
    	if (cnf.checkPacchetto((PkgPersonalDTO)pkg))
    		pkg_mgr.savePers((PkgPersonalDTO)pkg);
    }
    
    public PkgDTO ottieniPacchetto(String idpkg){
    	return pkg_mgr.getPKGtoModify(idpkg);
    }


	public ForClientInterface getPkg_mgr() {
		return pkg_mgr;
	}


	public void setPkg_mgr(ForClientInterface pkg_mgr) {
		this.pkg_mgr = pkg_mgr;
	}


	public EJBContext getContext() {
		return context;
	}


	public void setContext(EJBContext context) {
		this.context = context;
	}


	
	public List<PkgPersonalDTO> getPreferiteList(String own) {
		ArrayList<PkgPersonalDTO> p;
		p = new ArrayList<PkgPersonalDTO>();
	
		
		TypedQuery<Pacchetto_Personal> query = em.createQuery("SELECT a FROM Pacchetto_Personal a where a.Disponibilita = true and a.cliente = :own", Pacchetto_Personal.class)
														.setParameter("own", em.find(User.class, own));
		List<Pacchetto_Personal> resultList = query.getResultList();
		
		for(Pacchetto_Personal v: resultList)
			p.add(v.getDTO());
		return p;
	
	}
	
	public List<ComponenteDTO> getRegaliList(String id) {
		ArrayList<ComponenteDTO> p;
		p = new ArrayList<ComponenteDTO>();
	
		User ciccio= em.find(User.class, id);
		
		List<Componente> listaregali = ciccio.getListaRegali();
		
		for(Componente v: listaregali){
			if(!(v.isRegalo())){
				p.add(v.getDTO());
			}
			
		}
		return p;
	}

	@Override
	public void aggiungiAGiftList(String idcomp, String mail) {
		Componente regalo = em.find(Componente.class, idcomp);
		if(regalo==null){
			regalo = em.find(Trasporto.class, idcomp);
		}
		User uno = em.find(User.class, mail);
		/*List<Componente> lr = uno.getListaRegali();*/
		if(!(uno.getListaRegali().contains(regalo))){
			uno.getListaRegali().add(regalo);
			em.merge(regalo);
			em.merge(uno);	
		}	
	}
	

}
